
const {loginService,
    registryService,
    registryCheckName,
    getUserService,
    addUserService,
    delUserService,
    sureUserService,
    searchService,
    getPageService,
    getCountService
}=require("../service/user")

const {createToken,createCode}=require("../utils/util")

// 分页
const getPageController=async ctx=>{
    const {pageSize,limit}=ctx.request.query
    // const start=(pageSize-1)*limit
    const result=await getPageService(pageSize,limit)
    // 总数据的长度
    const count=await getCountService()

    if(result.length){
        ctx.body={
            code:"1001",
            msg:"查询分页成功",
            result,
            count:count[0]["count(*)"]
        }
    }else{
        ctx.body={
            code:"1002",
            msg:"查询分页失败",
            result,
            count:count[0]["count(*)"]
        }
    }
}

// 搜索
const searchController=async ctx=>{
    const {val}=ctx.request.query
    const result=await searchService(val)
    if(result.length){
        ctx.body={
            code:"1001",
            msg:"搜索成功",
            result
        }
    }else{
        ctx.body={
            code:"1001",
            msg:"暂无数据",
            result
        }
    }
}

// 编辑点击确定
const sureController=async ctx=>{
    const {id,username,nickname}=ctx.request.body
    const result=await sureUserService({id,username,nickname})
    if(result.affectedRows){
        ctx.body={
            code:"1001",
            msg:"编辑成功"
        }
    }else{
        ctx.body={
            code:"1002",
            msg:"编辑失败"
        }
    }
}

// 删除
const delUserController=async ctx=>{
    const {id}=ctx.request.query
    const result=await delUserService(id)
    if(result.affectedRows){
        ctx.body={
            code:"1001",
            msg:"删除成功"
        }
    }else{
        ctx.body={
            code:"1002",
            msg:"删除失败"
        }
    }
}

// 新增数据
const addUserController=async ctx=>{
    const {
        username,
        nickname,
        age,
        tel
    }=ctx.request.body
    const result=await addUserService({
        username,
        nickname,
        age,
        tel
    });
    if(result.affectedRows){
        ctx.body={
            code:"1001",
            msg:"添加成功"
        }
    }else{
        ctx.body={
            code:"1002",
            msg:"添加失败"
        }
    }
}

// 列表数据
const getUserController=async ctx=>{
    const result=await getUserService()
    if(result.length){
        ctx.body={
            code:"1001",
            msg:"获取用户成功",
            result
        }
    }else{
        ctx.body={
            code:"1001",
            msg:"暂无数据",
            result
        }
    }
}

// 验证码
const codeController=async ctx=>{
    const {data,text}=createCode();
    ctx.session.text=text //把数据存到session中
    ctx.body={
        code:"1001",
        svg:data,
        text
    }
}

//登录
// ctx 对象 请求体和响应体
const login=async ctx=>{
    // const sql=`select * from login`
    // const result=await query(sql)
    const {username,pwd,code}=ctx.request.body
    if(ctx.session.text!==code){
        ctx.body={
            code:"1002",
            msg:"验证码输入错误"
        }
    }else{
        // 通过sql 语句 查询条件
        const result=await loginService(username,pwd)
        // 响应前端的请求
        if(result.length){
            const token=createToken(result[0].id)
            ctx.body={
                code:"1001",
                msg:"登录成功",
                token
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"登录失败"
            }
        }
    }
}

// 注册
const registryCtroller=async ctx=>{
    let {username,pwd}=ctx.request.body
    const flag=await registryCheckName(username)
    if(flag){
        const result=await registryService(username,pwd)
        if(result.affectedRows){
            ctx.body={
                code:"1001",
                msg:"注册成功"
            }
        }else{
            ctx.body={
                code:"1002",
                msg:"注册失败"
            }
        }
    }else{
        ctx.body={
            code:"1002",
            msg:"该用户名已存在,请重新输入"
        }
    }
}

module.exports={
    login,
    registryCtroller,
    codeController,
    getUserController,
    addUserController,
    delUserController,
    sureController,
    searchController,
    getPageController
}